<template> 
    <el-card class="form-container" shadow="never">
    <el-form :model="value" :rules="rules" ref="productFrom" label-width="150px">
      <el-form-item label="手机名称：" prop="productName">
        <el-input v-model="value.productName"></el-input>
      </el-form-item>
      <el-form-item label="手机品牌：" prop="brandId">
        <el-select
          v-model="value.brandId"
          @change="handleBrandChange"
          placeholder="请选择品牌">
          <el-option
            v-for="item in brandOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机图片：" prop="pic">
        <single-upload v-model="value.pic"></single-upload>
      </el-form-item>
      <el-form-item label="上市时间：" prop="saleTime">
        <el-date-picker v-model="value.saleTime" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理器：" prop="processor">
        <el-input v-model="value.processor"></el-input>
      </el-form-item>
      <el-form-item label="测试内存组合：" prop="ramVersion">
        <el-input v-model="value.ramVersion"></el-input>
      </el-form-item>
      <el-form-item label="发售价格：">
        <el-input v-model="value.price"></el-input>
      </el-form-item>
      <el-form-item label="测试版本：" prop="systemVersion">
        <el-input v-model="value.systemVersion"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('productFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('productFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
  import {createProduct,getProduct,updateProduct} from '@/api/product';
  import {fetchList as fetchBrandList} from '@/api/brand'
  import SingleUpload from '@/components/Upload/singleUpload'
   const defaultProductParam = {
    brandId: null,
    brandName: '',
    productName: '',
    pic: '',
    saleTime: null,
    processor: '',
    ramVersion: '',
    price: 0,
    sort: 0,
    deleteStatus: 0,
    productCategoryType: 0,
    recommandStatus: 0,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };
  export default {
    name: 'ProductDetail',
    components:{SingleUpload},
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        value:Object.assign({}, defaultProductParam),
        brandOptions: [],
        rules: {
          productName: [
            {required: true, message: '请输入手机名称', trigger: 'blur'},
            {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
          ],
          brandId: [{required: true, message: '请选择商品品牌', trigger: 'blur'}],
          pic: [
            {required: true, message: '请输入手机图片', trigger: 'blur'}
          ],
          // sort: [
          //   {type: 'number', message: '排序必须为数字'}
          // ],
        }
      }
    },
    created() {
      if (this.isEdit) {
        this.getBrandList();
        getProduct(this.$route.query.id).then(response => {
          this.value = response.data;
        });
        
      }else{
        this.getBrandList();
        this.value = Object.assign({},defaultProductParam);
      }
    },
    methods: {
      getBrandList() {
        fetchBrandList({pageNum: 1, pageSize: 100}).then(response => {
          this.brandOptions = [];
          let brandList = response.data.list;
          for (let i = 0; i < brandList.length; i++) {
            this.brandOptions.push({label: brandList[i].brandName, value: brandList[i].id});
          }
        });
      },
      handleBrandChange(val) {
        let brandName = '';
        for (let i = 0; i < this.brandOptions.length; i++) {
          if (this.brandOptions[i].value === val) {
            brandName = this.brandOptions[i].label;
            break;
          }
        }
        this.value.brandName = brandName;
      },
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              if (this.isEdit) {
                updateProduct(this.$route.query.id, this.value).then(response => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: '修改成功',
                    type: 'success',
                    duration:1000
                  });
                  this.$router.back();
                });
              } else {
                createProduct(this.value).then(response => {
                  this.$refs[formName].resetFields();
                  this.value = Object.assign({},defaultProductParam);
                  this.$message({
                    message: '提交成功',
                    type: 'success',
                    duration:1000
                  });
                });
              }
            });

          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration:1000
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.value = Object.assign({},defaultProductParam);
      }
    }
  }
</script>
<style>
</style>
